package app.server.project.main.service.db.entity.user;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.util.MathUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

@Data
@Entity(name = "block_num")
@TableName("block_num")
@Comment(value = "区块类型")
public class BlockNumEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private int id;

    @TableField("num")
    @Column(name = "num")
    @Comment("区块号")
    private long num;

    @JsonIgnore
    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static BlockNumEntity of(int id) {
        BlockNumEntity userInfoEntity = new BlockNumEntity();
        userInfoEntity.setId(id);
        userInfoEntity.setSynDb(true);
        return userInfoEntity;
    }

    public void setNum(long num) {
        this.num = num;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(BlockNumEntity::getNum, this);
    }

    public void addNum() {
        var ret = MathUtil.canAddNum(this.num, 1);
        this.num += ret;
        this.setNum(this.num);
    }
}
